import configparser
import subprocess
import os

class Fingerprint:
    def __init__(self):
        self.fingerprint = ""
    
    def get_GPU_fingerprint(self):
        try:
            # 获取更详细的硬件信息
            result = subprocess.check_output(
                'wmic path win32_videocontroller get DeviceID,PNPDeviceID /value',
                shell=True
            ).decode('utf-8').strip()
            
            # 提取完整的硬件标识
            device_info = {}
            for line in result.split('\n'):
                if '=' in line:
                    key, value = line.split('=', 1)
                    device_info[key.strip()] = value.strip()
            
            # 组合关键信息作为指纹
            self.fingerprint = f"{device_info.get('DeviceID','')}|{device_info.get('PNPDeviceID','')}"
            return self.fingerprint
        except Exception as e:
            print(f"获取GPU序列号失败: {e}")
            return ""
    
    # def if_fingerprint_correct(self):
    #     config = configparser.ConfigParser()
    #     config_path = os.path.join(os.path.dirname(__file__), 'config.ini')
    #     config.read(config_path)
    #     stored_fingerprint = config.get('Fingerprint', 'device_fingerprint', fallback="")
    #     self.fingerprint = self.get_GPU_fingerprint()
    #     return self.fingerprint == stored_fingerprint
    
    # def store_fingerprint_to_config(self):
    #     self.fingerprint = self.get_GPU_fingerprint()
    #     config = configparser.ConfigParser()
    #     config_path = os.path.join(os.path.dirname(__file__), 'config.ini')
    #     config.read(config_path)
    #     config.set('Fingerprint', 'device_fingerprint', self.fingerprint)
    #     with open(config_path, 'w') as configfile:
    #         config.write(configfile)
    #     return self.fingerprint

if __name__ == "__main__":
    # 测试GPU指纹获取
    fp = Fingerprint()
    print("测试GPU指纹获取:")
    gpu_id = fp.get_GPU_fingerprint()
    if gpu_id:
        print(f"成功获取GPU指纹: {gpu_id}")
    else:
        print("获取GPU指纹失败")
    
    # 测试指纹比对
    print("\n测试指纹比对:")
    match_result = fp.if_fingerprint_correct()
    print(f"指纹比对结果: {'匹配成功' if match_result else '匹配失败'}")

    store_fingerprint = fp.store_fingerprint_to_config()
    print(f"指纹存储结果: {'存储成功' if store_fingerprint else '存储失败'}")

